package com.example.file_study.controller;

import com.alibaba.fastjson.JSON;
import com.example.file_study.model.Result;
import com.example.file_study.service.FileMangerService;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ExecutionException;


@RestController
@RequestMapping("/file")
public class FileMangerController {

    @Resource
    private   FileMangerService fileMangerService;


    @GetMapping("/downLoad")
    public void donloadFile( HttpServletResponse response){

        String path = "/in/demo.jpg";
        try {
            fileMangerService.downLoad(path,response);
        }catch (Exception e){
            response.setHeader("res", Base64.encodeBase64String(JSON.toJSONString(Result.fail("下载文件发生异常")).getBytes(StandardCharsets.UTF_8)));
        }
    }

    @GetMapping("/press")
    public void downLoadPress(HttpServletResponse response) throws IOException, ExecutionException, InterruptedException {
        String path = "C:\\data\\client\\2023\\黄靖文件学习\\合同资料\\黄靖A";
        fileMangerService.press(path,response);
    }

    @GetMapping("/unzip")
    public void unzipPress(){
        String zipPath = "F:\\file\\zip\\demoA.zip";
        String  desDir= "F:\\file\\temp\\";
        fileMangerService.unzipPress(zipPath,desDir);
    }

}
